package de.ifgi;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class EventsList extends ArrayList<Event> implements Parcelable {

	/**
	 * Generated serial version ID
	 */
	private static final long serialVersionUID = 1628431786433467935L;
	
	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

		@Override
		public EventsList createFromParcel(Parcel in) {
			return new EventsList(in);
		}

		@Override
		public EventsList[] newArray(int size) {
			return new EventsList[size];
		}
	};
	
	private static EventsList eventsList;

	private EventsList() {
		super();
	}

	public static EventsList getEventsList() {
		if (eventsList == null)
			eventsList = new EventsList();
		return eventsList;
	}
	
	public EventsList(Parcel in) {
		readFromParcel(in);
	}

	private void readFromParcel(Parcel in) {
		this.clear();
		int size = in.readInt();
		for (int i=0; i<size; i++) {
			Event e = new Event();
			e.setEvtType(in.readString());
			e.setEvtLevel(in.readString());
			double[] pos = new double[2];
			in.readDoubleArray(pos);
			e.setEstPosition(new Point(pos[0], pos[1]));
			this.add(e);
		}
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		int size = this.size();
		dest.writeInt(size);
		for (int i=0; i<size; i++) {
			Event e = this.get(i);
			dest.writeString(e.getEvtType());
			dest.writeString(e.getEvtLevel());
			double[] pos = {e.getEstPosition().getX(), e.getEstPosition().getY()};
			dest.writeDoubleArray(pos);
		}
	}

	public void updateEvent(Event event) {
		int evtId = 0;
		String evtName = event.getEvtType();
		for (Event e : this) {
			if (e.getEvtType().equalsIgnoreCase(evtName))
				evtId = this.indexOf(e);
		}
		this.set(evtId, event);
	}

	public boolean hasEvent(Event event) {
		boolean hasEvent = false;
		String evtName = event.getEvtType();
		for (Event e : this) {
			if (e.getEvtType().equalsIgnoreCase(evtName)) {
				hasEvent = true;
			}
		}
		return hasEvent;
	}
	
}
